var app = getApp(),
  fn = app.fn;
var QQMapWX = require("../../utils/qqmap-wx-jssdk.js");
var qqmapsdk;
Page({
  data: {
    sortStatus: false,
    filterStatus: false,
    modalStatus: false,
    sortIndex: 0,   //  
    filterIndex: '',
    sortType: '',
    hotPro: {
      data: [],
      loading: false,
      loadend: false,
      reqData: {
        subTypeId: 0,
        sorting: 5,
        page: 0
      }
    },
    filterData: [
      {
        "type": 'take_off',
        "text": '起送费(单选)',
        'selected': '',
        'moreselcted': [],
        "content": [{
          'name': '全部',
          'value': '-1'
        },
        {
          'name': '0起送费',
          'value': '0'
        },
        {
          'name': '非0起送费',
          'value': '1'
        }
        ]
      },
      {
        "type": 'is_guanzhu',
        "text": '关注(单选)',
        "content": [{
          'name': '全部',
          'value': '-1'
        },
        {
          'name': '不关注',
          'value': '0'
        },
        {
          'name': '关注',
          'value': '1'
        }
        ]
      },
      {
        "type": 'favourable',
        "text": '优惠活动(单选)',
        "content": [{
          'name': '全部',
          'value': '-1'
        },
        {
          'name': '没有优惠活动',
          'value': '0'
        },
        {
          'name': '有优惠活动',
          'value': '1'
        }
        ]
      },
      {
        "type": 'tags',
        "text": '店铺标签(单选)',
        "content": [{
          'name': '全部',
          'value': '-1'
        },
        {
          'name': '没有标签',
          'value': '0'
        },
        {
          'name': '有标签',
          'value': '1'
        }
        ]
      },
      {
        "type": 'rank_level',
        "text": '店铺级别(可多选)',
        "more": true,
        "content": [{
          'name': '初级',
          'value': '0'
        },
        {
          'name': '中级',
          'value': '1'
        },
        {
          'name': '高级',
          'value': '2'
        }
        ]
      },
      {
        "type": 'shipping',
        "text": '配送方式(单选)',
        "content": [{
          'name': '全部',
          'value': '-1'
        },
        {
          'name': '平台配送',
          'value': 'tc_express'
        },
        {
          'name': '商家配送',
          'value': 'sj_express'
        }
        ]
      },

    ]
  },

  onLoad: function () {

    this.setData({
      "hotPro.reqData.lng": app.globalData.lng,
      "hotPro.reqData.lat": app.globalData.lat,
    });
    this.getInitData();

  },
  setFormVal: function (e) {
    var val = e.detail.value;
    this.setData({
      formVal: val
    });
  },

  getInitData2: function (callBack) {
    var that = this,
      flag = 0;
  
    var pageNo = ++that.data.hotPro.reqData.page;
    that.setData({
      "hotPro.reqData.show_goods": 1,
      "hotPro.reqData.page": pageNo,
      "hotPro.reqData.user_id": app.globalData.uid,
      "hotPro.reqData.act": 'takeout_list',
      "hotPro.reqData.lng": app.globalData.lng,

      "hotPro.reqData.location_status": '2',
      "hotPro.reqData.lat": app.globalData.lat,
    });
    fn.apiData({
      url: 'stores.php',

      data: that.data.hotPro.reqData,
      success: function (res) {
        if (res.data.code == 0) {
          var newData = res.data.data;
          if (newData.length == 0 || !newData) {
        
            that.setData({
              'hotPro.loadend': true
            });
          } else {
      
            newData = that.data.hotPro.data.concat(newData);
            that.setData({
              'hotPro.data': newData,
              'hotPro.loading': false
            });
          }

     
          wx.hideToast();
        }
      }
    });









    function apiFinish() {
      ++flag;
      if (flag >= 3) {
        wx.hideToast();
        if (!!callBack) callBack();
      }
    }
  },

  getInitData: function (callBack) {
    var that = this,
      flag = 0;
   
  var  pageNo = ++that.data.hotPro.reqData.page;
    that.setData({
      "hotPro.reqData.show_goods": 1,
      "hotPro.reqData.page": 1,
      "hotPro.reqData.user_id": app.globalData.uid,
      "hotPro.reqData.act": 'takeout_list',
      "hotPro.reqData.lng": app.globalData.lng,

      "hotPro.reqData.location_status": '2',
      "hotPro.reqData.lat": app.globalData.lat,
    });
    fn.apiData({
      url: 'stores.php',

      data: that.data.hotPro.reqData,
      success: function (res) {
        if (res.data.code == 0) {
          var newData = res.data.data;
          if (newData == null || !newData) {
      
            that.setData({
              'hotPro.data': newData,
              'hotPro.loadend': true
            });
            wx.hideToast();
          } else {
         

            wx.hideToast();

            that.setData({
              'hotPro.data': newData,
              'hotPro.loading': false
            });
          }
        }
      }
    });
  







   
  
    function apiFinish() {
      ++flag;
      if (flag >= 3) {
        wx.hideToast();
        if (!!callBack) callBack();
      }
    }
  },

  formSubmit: function () {
    var that=this;
    var kw = fn.trim(this.data.formVal);
    if (kw == "" || !kw) {
      return false;
    }else{

      that.setData({

        "hotPro.reqData.keywords": kw,
      });

    }
    this.getInitData();

  },

  onReady: function () {

  },
  showSortClose: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.id;
 
    this.setData({
      sortStatus: (!that.data.sortStatus),
      filterStatus: false,
      sortIndex: index
    });
    if (this.data.sortStatus) {
      this.showModal();
    } else {
      this.hideModal();
    }
  },

  showFilterClose: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.id;
    this.setData({
      filterStatus: (!that.data.filterStatus),
      sortStatus: false,
      sortIndex: index
    });
    if (this.data.filterStatus) {
      this.showModal();
    } else {
      this.hideModal();
    }
  },
  showFilterClose2: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.id;
    this.setData({
      filterStatus: (!that.data.filterStatus),
      sortStatus: false,
      sortIndex: index
    });
   that.getInitData();
    if (this.data.filterStatus) {
      this.showModal();
    } else {
      this.hideModal();
    }
  },
  sortType: function (e) {


    this.setData({

      "hotPro.loadend": false,
    });
    let sortType = e.currentTarget.dataset.type;
    let filterIndex = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let selectIndex = e.currentTarget.dataset.selectindex;

    let filterData = 'filterData[' + index + '].selected';   
    let filter_content = 'filterData[' + index + '].content[' + selectIndex + '].selected';

    let moreFilterData = 'filterData[' + index + '].content[' + selectIndex + '].selected';
    let that = this;
  
    if (this.data.sortStatus) {
      this.setData({
        sortStatus: (!that.data.sortStatus),
        filterStatus: false,
        sortType: sortType,
      });

      that.setData({
        "hotPro.reqData.type": sortType,
        "hotPro.reqData.page": 1,
      });
      
      this.setData({
        sortIndex: index
      });
      that.getInitData();
      this.hideModal();
    }
    that.setData({
      "hotPro.reqData.page": 1,
    });
    switch (sortType) {
      case 'take_off': 
        that.setData({
          "hotPro.reqData.take_off": filterIndex,
        });
        break;
      case 'is_guanzhu':  
        that.setData({

          "hotPro.reqData.is_guanzhu": filterIndex,
        });
        break;
      case 'favourable':  

        that.setData({

          "hotPro.reqData.favourable": filterIndex,
        });
        break;
      case 'tags': 
        that.setData({

          "hotPro.reqData.tags": filterIndex,
        });
        break;
      case 'rank_level':  
        that.setData({

          "hotPro.reqData.rank_level": filterIndex,
        });


        break;
      case 'shipping': 
        that.setData({

          "hotPro.reqData.shipping": filterIndex,
        });


        break;
    }

   
    if (this.data.filterStatus) {
     
      let attmoreFlag = this.data.filterData[index].content[selectIndex].selected;
      if (sortType == 'rank_level') {
        this.setData({
          sortType: sortType,
          [moreFilterData]: !attmoreFlag
        });
     
        return;
      }
   
      let attFlag = this.data.filterData[index].content[selectIndex].selected; 
      for (var i = 0; i < this.data.filterData[index].content.length; i++) {
        this.data.filterData[index].content[i].selected = false;
      }
      if (attFlag) {


        switch (sortType) {
          case 'take_off':  
            that.setData({
              "hotPro.reqData.take_off": '',
            });
            break;
          case 'is_guanzhu': 
            that.setData({

              "hotPro.reqData.is_guanzhu": '',
            });
            break;
          case 'favourable': 

            that.setData({

              "hotPro.reqData.favourable": '',
            });
            break;
          case 'tags':  
            that.setData({

              "hotPro.reqData.tags": '',
            });
            break;
          case 'rank_level':  
            that.setData({

              "hotPro.reqData.rank_level": '',
            });


            break;
          case 'shipping': 
            that.setData({

              "hotPro.reqData.shipping": '',
            });


            break;
        }

      }
      this.setData({
        sortType: sortType,
        [filterData]: filterIndex,
        [filter_content]: !attFlag
      });
    }
  },


  distanceMark: function (e) {
    var that = this;

    that.setData({

      "hotPro.loadend": false,
    });
    that.setData({

      "hotPro.reqData": "",
    });
    let index = e.currentTarget.dataset.id;

    var id = e.currentTarget.dataset.id,
      url;
    switch (id) {
      case '0':  
        url = '../orderList/orderList?orderStatus=0';
        break;
      case '1':  
        url = '../orderList/orderList?orderStatus=1';
        break;
      case '2': 

        that.setData({

          "hotPro.reqData.type": 'distance1',
        });
        break;
      case '3':  
        that.setData({

          "hotPro.reqData.type": 'pingfen',
        });
        break;
      case '4':  
        that.setData({

          "hotPro.reqData.type": 'order_num',
        });


        break;
    }
    this.setData({
      sortIndex: index
    });
    
    that.getInitData();
  },

  showModal: function () {
   
    var animation = wx.createAnimation({
      duration: 500,        
      timingFunction: "step-end",
      delay: 100             
    })
    this.animation = animation;
  
    this.setData({
    
      animationData: animation.export(),
      modalStatus: true  
    });
  },

  onReachBottom: function () {
    this.getInitData2();
  },

  hideModal: function () {
   
    var animation = wx.createAnimation({
      duration: 1500,         
      timingFunction: "ease",
      delay: 0              
    })
    this.animation = animation;

   
    this.setData({
      
      animationData: animation.export(),
      modalStatus: false  
    });
  }
})
